<?
$pagename=LA_YOUR_ADS;
include_once("header_inc.php");
$my_items = 1;
$lDoNotCreateTemplate=1;
include_once("member_header.php");

check_valid_user("");
$valid_user = $_SESSION["valid_user"];
$mainType=getParam("mainType",ID_CAT_COMPRA);
$tplList=new TplLoad;
$tplList->assign("set_renew_ad",$set_renew_ad);
$tplList->assign("set_video_upload", "$set_video_upload");
$tplList->assign("set_doc_upload", "$set_doc_upload");
if(getParam("checkCredit","0")==1){
	
	$arrayAux = getPaqueteMainTypeFromAdId(getParam("ad_id",0),true);
	$mainType= $arrayAux['mainType'];
	$package_id = $arrayAux['package_id'];
	if(habilitarAvisosDisabled($mainType,$_SESSION["valid_user"],$package_id)==-1)
		$tplList->assign("sorryNoCredits",EF_SORRY_NO_CREDITS);
}

if (getParam("deleteid",""))
{
          $deleteid=round($_REQUEST["deleteid"]); 
          $owner_check = mysql_fetch_array(q("select ad_owner from $ads_tbl where ad_id=$deleteid")); 
          if ($valid_user==$owner_check["ad_owner"]) 
          {
			$userside=1; 
			if (getParam("confirmed","")==1)
			{
				delete_ad(round(getParamInt("deleteid","")));
				$tplList->assign("ad_deleted",1);
			}
			else 
			{
				$tplList->assign("please_confirm",round(getParam("deleteid","")));
			}
          }
}

if (getParam("soldid",""))
{
	$sql_sold = "update $ads_tbl set ad_is_sold = 1 where ad_owner = $valid_user AND ad_id=".round(getParamInt("soldid",""));
    $result = q($sql_sold);
    $msg = formatString(LA_SOLD_MARKED,array(getParam("soldid","")));
	$tplList->assign("message",$msg);
}

if (getParam("unsoldid",""))
{
	$sql_sold = "update $ads_tbl set ad_is_sold = 0 where ad_owner = $valid_user AND ad_id=".round(getParamInt("unsoldid",""));
	$result = q($sql_sold);
	$msg =  formatString(LA_UNSOLD_MARKED,array(getParam("unsoldid","")));
	$tplList->assign("message",$msg);
}

if (getParam("renew_days",""))
{
	$sql="select ad_date_expire,ad_title,ad_cat_id,ad_id,user_name,user_email,user_id,mv_package_id from $ads_tbl,$usr_tbl,mvial_ad_extra_options where ad_id=mv_ad_id  and ad_owner=user_id AND ad_id=" . getParamInt("renew_id","") . " AND ad_owner=" . $_SESSION["valid_user"] . " AND ad_owner_notified=1";
	$res=q($sql);
	$row=mysql_fetch_array($res);
	$lCurrentExpireDate=$row["ad_date_expire"];
	$lSiteTitle=$row["ad_title"];
	$lad_id=$row["ad_id"];
	$lName=$row["user_name"];
	$lUserId=$row["user_id"];
	$lEmail=$row["user_email"];
	$lCatId=$row["ad_cat_id"];
	$packageId = $row["mv_package_id"];
	
	$lNewExpireDate=addDaysToTimeStamp(getParamInt("renew_days","30"),$lCurrentExpireDate);
	$lStop=0;
	
	if ($set_payments && !getExpireStatus($lUserId))
	{
		$sql_top = "select cat_cost from $cat_tbl where cat_id = $lCatId";
		$res_cat=q($sql_top);
		$row_cost=mysql_fetch_array($res_cat);
		$lCatCost=$row_cost["cat_cost"];
		$sql="select pay_credits,pay_bold,pay_premium from mvial_user_credits where user_id=" . $lUserId." and mainType=".$mainType." and package_id=".$packageId;
		$res_user=q($sql);
		$row_user=mysql_fetch_array($res_user);
		$lPayCredits=$row_user["pay_credits"];
		if(empty($lPayCredits)){
			$lPayCredits=0;
		}
		
		if ($lCatCost>0)
		{
			if ($lPayCredits>=$lCatCost)
			{
				$sql="update $user_credits set pay_credits = pay_credits - $lCatCost where pay_credits>0";
				$sql.=" AND user_id = ". $lUserId." and mainType=".$mainType." and package_id=".$packageId;		
				$res_pay=q($sql);
			}
			else
			{
				$tplList->assign("pay_out_of_credits",1);
				$lStop=1;
			}
		}
	}
	
	if (!$lStop) // If no credits left, stop from letting user renew ad
	{
		if (!$lCurrentExpireDate)
			$tplList->assign("message",LA_NOT_WARNED);
		else 
		{
			$sql_renew = "update $ads_tbl set ad_date_expire='$lNewExpireDate',ad_owner_notified='0' where ad_owner = $lUserId AND ad_id= ". getParamInt("renew_id","");
			$result = q($sql_renew);
	   		$tplList->assign("message", formatString(LA_AD_IS_RENEWED,array("".getParamInt("renew_days","30") . "")));
	   		
	   		
	   		$lTitle="Information: Ad owner has renewed their ad";
	   		$lBody="This is just a notification: Ad owner $lName has just renewed their ad titled $lSiteTitle with " . getParamInt("renew_days","30") . " days.\n\nTheir ad is at http://$url/detail.php?id=$lad_id.\n\n";
	
	   		if ($set_inform_admin_renew)
	   			sendEmail($set_webmaster_address,$lEmail,$lTitle,$lBody);
		}
	}
}
require("links.php");
include_once("member_footer.php");
include_once("footer_inc.php");
?>